﻿using System;
using System.IO;
using System.Text;
using BestHTTP;
using UnityEngine;

namespace Networking.Runtime
{
    public static class DownLoadExtensions
    {
        public static void PrepareFileDirectory(string path)
        {
            if (File.Exists(path))
            {
                return;
            }
            string directory = Path.GetDirectoryName(path);
            PrepareDirectory(directory);
        }
        
        public static void DeleteFile(string path)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }
        
        public static void PreparePath(string path)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            string directory = Path.GetDirectoryName(path);
            PrepareDirectory(directory);
        }

        public static void PrepareDirectory(string directory)
        {
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
        }

        public static void MoveTo(string filePath, string destPath)
        {
            if (File.Exists(filePath))
            {
                PreparePath(destPath);
                File.Move(filePath, destPath);
            }
        }

        public static bool IsFileDownloaded(string path, string md5)
        {
            return File.Exists(path) && md5.Equals(GetMD5HashFromFile(path), StringComparison.OrdinalIgnoreCase);
        }

        public static long GetFileSize(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return 0;
            }

            FileInfo fileInfo = new FileInfo(filePath);
            return fileInfo.Length;
        }

        public static string GetMD5HashFromFile(string path)
        {
            try
            {
                using (FileStream file = new FileStream(path, FileMode.Open))
                {
                    using (System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider())
                    {
                        byte[] retVal = md5.ComputeHash(file);
                        file.Close();
                        StringBuilder result = new StringBuilder();
                        for (int i = 0; i < retVal.Length; i++)
                        {
                            result.Append(retVal[i].ToString("x2"));
                        }

                        return result.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
            }
        }
        
        public static void Clear<T>(this System.Collections.Concurrent.ConcurrentQueue<T> queue)
        {
            T result;
            while (queue.TryDequeue(out result))
                ;
        }
        // public static long GetHeaderDownLoadSize(this HTTPResponse resp)
        // {
        //     var range = resp.GetRange();
        //     if (range != null)
        //     {
        //         return range.ContentLength;
        //     }
        //     else
        //     {
        //         var contentLength = resp.GetFirstHeaderValue(DownLoadSetting.HEADER_CONTENT_LENGTH);
        //         if (contentLength != null)
        //         {
        //             if (long.TryParse(contentLength, out var length))
        //             {
        //                 return length;
        //             }
        //         }
        //         Debug.LogWarning("Unknown file size!");
        //         return 0;
        //     }
        // }
    }
}